// CHESS
function sendchess() {
	for(i in client_state.chessgames) {
		game_name = "ChessGame_" + i;
		fpick[i] = "";
		if($(game_name+"_cont")===null) {
			$("extra_windows").innerHTML += "<div id="+game_name+"_cont class=outerWindow style=\'width:374px;\'><div class=windowHeader onmousedown=\"dragStart(event, \'"+game_name+"_cont\')\" style=\'width:381px;\'>"+game_name+"<div class=closeWindow id=closer onclick=\"chess_quit('"+i+"')\">X</div></div><div id="+game_name+" style='width:100%;max-height:381px;'></div></div>";
		}
		$(game_name+"_cont").style.zIndex = ++dragObj.zIndex;
		
		show_chess_board(client_state.chessgames[i],i);
	}
	client_state.chessgames = null;
}

function show_chess_board(board,i) {
	tmp =
	"<div id="+i+"A8 class=white_boardsquare><img onclick=\"choosepiece('A8',"+i+")\" class=chesspiece src=images/chess/"+(board[1][8].image || "no.png")+" /></div>"+
	"<div id="+i+"B8 class=black_boardsquare><img onclick=\"choosepiece('B8',"+i+")\" class=chesspiece src=images/chess/"+(board[2][8].image || "no.png")+" /></div>"+
	"<div id="+i+"C8 class=white_boardsquare><img onclick=\"choosepiece('C8',"+i+")\" class=chesspiece src=images/chess/"+(board[3][8].image || "no.png")+" /></div>"+
	"<div id="+i+"D8 class=black_boardsquare><img onclick=\"choosepiece('D8',"+i+")\" class=chesspiece src=images/chess/"+(board[4][8].image || "no.png")+" /></div>"+
	"<div id="+i+"E8 class=white_boardsquare><img onclick=\"choosepiece('E8',"+i+")\" class=chesspiece src=images/chess/"+(board[5][8].image || "no.png")+" /></div>"+
	"<div id="+i+"F8 class=black_boardsquare><img onclick=\"choosepiece('F8',"+i+")\" class=chesspiece src=images/chess/"+(board[6][8].image || "no.png")+" /></div>"+
	"<div id="+i+"G8 class=white_boardsquare><img onclick=\"choosepiece('G8',"+i+")\" class=chesspiece src=images/chess/"+(board[7][8].image || "no.png")+" /></div>"+
	"<div id="+i+"H8 class=black_boardsquare><img onclick=\"choosepiece('H8',"+i+")\" class=chesspiece src=images/chess/"+(board[8][8].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>8</div>"+
	"<br class=clear />"+
	"<div id="+i+"A7 class=black_boardsquare><img onclick=\"choosepiece('A7',"+i+")\" class=chesspiece src=images/chess/"+(board[1][7].image || "no.png")+" /></div>"+
	"<div id="+i+"B7 class=white_boardsquare><img onclick=\"choosepiece('B7',"+i+")\" class=chesspiece src=images/chess/"+(board[2][7].image || "no.png")+" /></div>"+
	"<div id="+i+"C7 class=black_boardsquare><img onclick=\"choosepiece('C7',"+i+")\" class=chesspiece src=images/chess/"+(board[3][7].image || "no.png")+" /></div>"+
	"<div id="+i+"D7 class=white_boardsquare><img onclick=\"choosepiece('D7',"+i+")\" class=chesspiece src=images/chess/"+(board[4][7].image || "no.png")+" /></div>"+
	"<div id="+i+"E7 class=black_boardsquare><img onclick=\"choosepiece('E7',"+i+")\" class=chesspiece src=images/chess/"+(board[5][7].image || "no.png")+" /></div>"+
	"<div id="+i+"F7 class=white_boardsquare><img onclick=\"choosepiece('F7',"+i+")\" class=chesspiece src=images/chess/"+(board[6][7].image || "no.png")+" /></div>"+
	"<div id="+i+"G7 class=black_boardsquare><img onclick=\"choosepiece('G7',"+i+")\" class=chesspiece src=images/chess/"+(board[7][7].image || "no.png")+" /></div>"+
	"<div id="+i+"H7 class=white_boardsquare><img onclick=\"choosepiece('H7',"+i+")\" class=chesspiece src=images/chess/"+(board[8][7].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>7</div>"+
	"<br class=clear />"+
	"<div id="+i+"A6 class=white_boardsquare><img onclick=\"choosepiece('A6',"+i+")\" class=chesspiece src=images/chess/"+(board[1][6].image || "no.png")+" /></div>"+
	"<div id="+i+"B6 class=black_boardsquare><img onclick=\"choosepiece('B6',"+i+")\" class=chesspiece src=images/chess/"+(board[2][6].image || "no.png")+" /></div>"+
	"<div id="+i+"C6 class=white_boardsquare><img onclick=\"choosepiece('C6',"+i+")\" class=chesspiece src=images/chess/"+(board[3][6].image || "no.png")+" /></div>"+
	"<div id="+i+"D6 class=black_boardsquare><img onclick=\"choosepiece('D6',"+i+")\" class=chesspiece src=images/chess/"+(board[4][6].image || "no.png")+" /></div>"+
	"<div id="+i+"E6 class=white_boardsquare><img onclick=\"choosepiece('E6',"+i+")\" class=chesspiece src=images/chess/"+(board[5][6].image || "no.png")+" /></div>"+
	"<div id="+i+"F6 class=black_boardsquare><img onclick=\"choosepiece('F6',"+i+")\" class=chesspiece src=images/chess/"+(board[6][6].image || "no.png")+" /></div>"+
	"<div id="+i+"G6 class=white_boardsquare><img onclick=\"choosepiece('G6',"+i+")\" class=chesspiece src=images/chess/"+(board[7][6].image || "no.png")+" /></div>"+
	"<div id="+i+"H6 class=black_boardsquare><img onclick=\"choosepiece('H6',"+i+")\" class=chesspiece src=images/chess/"+(board[8][6].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>6</div>"+
	"<br class=clear />"+
	"<div id="+i+"A5 class=black_boardsquare><img onclick=\"choosepiece('A5',"+i+")\" class=chesspiece src=images/chess/"+(board[1][5].image || "no.png")+" /></div>"+
	"<div id="+i+"B5 class=white_boardsquare><img onclick=\"choosepiece('B5',"+i+")\" class=chesspiece src=images/chess/"+(board[2][5].image || "no.png")+" /></div>"+
	"<div id="+i+"C5 class=black_boardsquare><img onclick=\"choosepiece('C5',"+i+")\" class=chesspiece src=images/chess/"+(board[3][5].image || "no.png")+" /></div>"+
	"<div id="+i+"D5 class=white_boardsquare><img onclick=\"choosepiece('D5',"+i+")\" class=chesspiece src=images/chess/"+(board[4][5].image || "no.png")+" /></div>"+
	"<div id="+i+"E5 class=black_boardsquare><img onclick=\"choosepiece('E5',"+i+")\" class=chesspiece src=images/chess/"+(board[5][5].image || "no.png")+" /></div>"+
	"<div id="+i+"F5 class=white_boardsquare><img onclick=\"choosepiece('F5',"+i+")\" class=chesspiece src=images/chess/"+(board[6][5].image || "no.png")+" /></div>"+
	"<div id="+i+"G5 class=black_boardsquare><img onclick=\"choosepiece('G5',"+i+")\" class=chesspiece src=images/chess/"+(board[7][5].image || "no.png")+" /></div>"+
	"<div id="+i+"H5 class=white_boardsquare><img onclick=\"choosepiece('H5',"+i+")\" class=chesspiece src=images/chess/"+(board[8][5].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>5</div>"+
	"<br class=clear />"+
	"<div id="+i+"A4 class=white_boardsquare><img onclick=\"choosepiece('A4',"+i+")\" class=chesspiece src=images/chess/"+(board[1][4].image || "no.png")+" /></div>"+
	"<div id="+i+"B4 class=black_boardsquare><img onclick=\"choosepiece('B4',"+i+")\" class=chesspiece src=images/chess/"+(board[2][4].image || "no.png")+" /></div>"+
	"<div id="+i+"C4 class=white_boardsquare><img onclick=\"choosepiece('C4',"+i+")\" class=chesspiece src=images/chess/"+(board[3][4].image || "no.png")+" /></div>"+
	"<div id="+i+"D4 class=black_boardsquare><img onclick=\"choosepiece('D4',"+i+")\" class=chesspiece src=images/chess/"+(board[4][4].image || "no.png")+" /></div>"+
	"<div id="+i+"E4 class=white_boardsquare><img onclick=\"choosepiece('E4',"+i+")\" class=chesspiece src=images/chess/"+(board[5][4].image || "no.png")+" /></div>"+
	"<div id="+i+"F4 class=black_boardsquare><img onclick=\"choosepiece('F4',"+i+")\" class=chesspiece src=images/chess/"+(board[6][4].image || "no.png")+" /></div>"+
	"<div id="+i+"G4 class=white_boardsquare><img onclick=\"choosepiece('G4',"+i+")\" class=chesspiece src=images/chess/"+(board[7][4].image || "no.png")+" /></div>"+
	"<div id="+i+"H4 class=black_boardsquare><img onclick=\"choosepiece('H4',"+i+")\" class=chesspiece src=images/chess/"+(board[8][4].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>4</div>"+
	"<br class=clear />"+
	"<div id="+i+"A3 class=black_boardsquare><img onclick=\"choosepiece('A3',"+i+")\" class=chesspiece src=images/chess/"+(board[1][3].image || "no.png")+" /></div>"+
	"<div id="+i+"B3 class=white_boardsquare><img onclick=\"choosepiece('B3',"+i+")\" class=chesspiece src=images/chess/"+(board[2][3].image || "no.png")+" /></div>"+
	"<div id="+i+"C3 class=black_boardsquare><img onclick=\"choosepiece('C3',"+i+")\" class=chesspiece src=images/chess/"+(board[3][3].image || "no.png")+" /></div>"+
	"<div id="+i+"D3 class=white_boardsquare><img onclick=\"choosepiece('D3',"+i+")\" class=chesspiece src=images/chess/"+(board[4][3].image || "no.png")+" /></div>"+
	"<div id="+i+"E3 class=black_boardsquare><img onclick=\"choosepiece('E3',"+i+")\" class=chesspiece src=images/chess/"+(board[5][3].image || "no.png")+" /></div>"+
	"<div id="+i+"F3 class=white_boardsquare><img onclick=\"choosepiece('F3',"+i+")\" class=chesspiece src=images/chess/"+(board[6][3].image || "no.png")+" /></div>"+
	"<div id="+i+"G3 class=black_boardsquare><img onclick=\"choosepiece('G3',"+i+")\" class=chesspiece src=images/chess/"+(board[7][3].image || "no.png")+" /></div>"+
	"<div id="+i+"H3 class=white_boardsquare><img onclick=\"choosepiece('H3',"+i+")\" class=chesspiece src=images/chess/"+(board[8][3].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>3</div>"+
	"<br class=clear />"+
	"<div id="+i+"A2 class=white_boardsquare><img onclick=\"choosepiece('A2',"+i+")\" class=chesspiece src=images/chess/"+(board[1][2].image || "no.png")+" /></div>"+
	"<div id="+i+"B2 class=black_boardsquare><img onclick=\"choosepiece('B2',"+i+")\" class=chesspiece src=images/chess/"+(board[2][2].image || "no.png")+" /></div>"+
	"<div id="+i+"C2 class=white_boardsquare><img onclick=\"choosepiece('C2',"+i+")\" class=chesspiece src=images/chess/"+(board[3][2].image || "no.png")+" /></div>"+
	"<div id="+i+"D2 class=black_boardsquare><img onclick=\"choosepiece('D2',"+i+")\" class=chesspiece src=images/chess/"+(board[4][2].image || "no.png")+" /></div>"+
	"<div id="+i+"E2 class=white_boardsquare><img onclick=\"choosepiece('E2',"+i+")\" class=chesspiece src=images/chess/"+(board[5][2].image || "no.png")+" /></div>"+
	"<div id="+i+"F2 class=black_boardsquare><img onclick=\"choosepiece('F2',"+i+")\" class=chesspiece src=images/chess/"+(board[6][2].image || "no.png")+" /></div>"+
	"<div id="+i+"G2 class=white_boardsquare><img onclick=\"choosepiece('G2',"+i+")\" class=chesspiece src=images/chess/"+(board[7][2].image || "no.png")+" /></div>"+
	"<div id="+i+"H2 class=black_boardsquare><img onclick=\"choosepiece('H2',"+i+")\" class=chesspiece src=images/chess/"+(board[8][2].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>2</div>"+
	"<br class=clear />"+
	"<div id="+i+"A1 class=black_boardsquare><img onclick=\"choosepiece('A1',"+i+")\" class=chesspiece src=images/chess/"+(board[1][1].image || "no.png")+" /></div>"+
	"<div id="+i+"B1 class=white_boardsquare><img onclick=\"choosepiece('B1',"+i+")\" class=chesspiece src=images/chess/"+(board[2][1].image || "no.png")+" /></div>"+
	"<div id="+i+"C1 class=black_boardsquare><img onclick=\"choosepiece('C1',"+i+")\" class=chesspiece src=images/chess/"+(board[3][1].image || "no.png")+" /></div>"+
	"<div id="+i+"D1 class=white_boardsquare><img onclick=\"choosepiece('D1',"+i+")\" class=chesspiece src=images/chess/"+(board[4][1].image || "no.png")+" /></div>"+
	"<div id="+i+"E1 class=black_boardsquare><img onclick=\"choosepiece('E1',"+i+")\" class=chesspiece src=images/chess/"+(board[5][1].image || "no.png")+" /></div>"+
	"<div id="+i+"F1 class=white_boardsquare><img onclick=\"choosepiece('F1',"+i+")\" class=chesspiece src=images/chess/"+(board[6][1].image || "no.png")+" /></div>"+
	"<div id="+i+"G1 class=black_boardsquare><img onclick=\"choosepiece('G1',"+i+")\" class=chesspiece src=images/chess/"+(board[7][1].image || "no.png")+" /></div>"+
	"<div id="+i+"H1 class=white_boardsquare><img onclick=\"choosepiece('H1',"+i+")\" class=chesspiece src=images/chess/"+(board[8][1].image || "no.png")+" /></div>"+
	"<div class=right_chessborder>1</div>"+
	"<br class=clear />"+
	"<div class=bottom_chessborder>A</div>"+
	"<div class=bottom_chessborder>B</div>"+
	"<div class=bottom_chessborder>C</div>"+
	"<div class=bottom_chessborder>D</div>"+
	"<div class=bottom_chessborder>E</div>"+
	"<div class=bottom_chessborder>F</div>"+
	"<div class=bottom_chessborder>G</div>"+
	"<div class=bottom_chessborder>H</div>";
	
	log(tmp,game_name,true);
	tmp = "";
}

fpick = [];
oldbrdr = [];
function choosepiece(id,i) {
	if(fpick[i] === id) {
		fpick[i] = "";
		$(i+id).style.border = oldbrdr[i];
		return;
	}
	
	if(fpick[i]!=="") {
		$(i+fpick[i]).style.border = oldbrdr[i];
		try{ socket.send("chessmove " + i + " " + fpick[i] + " " + id); } catch(ex){ log(ex); }
		fpick[i] = "";
		return;
	}
	
	fpick[i] = id;
	oldbrdr[i] = $(i+id).style.border;
	$(i+id).style.border = "1px solid black";
}

function chess_quit(msg) {
	try{ socket.send("chessquit "+msg); } catch(ex){ log(ex); }
	game_name = "ChessGame_" + msg.substring(msg.search(" "));
	pushtoback(game_name+"_cont");
}